package com.example.beson.androiddemo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beson.androiddemo.login.mvc.controller.MVCLoginController;
import com.example.beson.androiddemo.login.mvc.model.UserInfo;
import com.example.beson.androiddemo.login.mvc.view.MVCILoginView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MVCLoginActivity extends AppCompatActivity implements MVCILoginView {
    @BindView(R.id.login_progressbar)
    ProgressBar progressBar;
    @BindView(R.id.login_title)
    TextView loginTitle;
    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_pwd)
    EditText loginPwd;
    @BindView(R.id.login_submit)
    Button loginSubmit;
    protected MVCLoginController mController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvplogin);
        ButterKnife.bind(this);
        mController=new MVCLoginController(MVCLoginActivity.this);
        loginTitle.setText("MVC  登录");
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissLoading() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public Context getContext() {
        return getApplicationContext();
    }

    /**
     * 在登录成功之后可以修改UI以及进行跳转操作
     * @param msg
     */
    @Override
    public void loginSuccess(UserInfo msg) {
        showMsg("登录成功   "   +msg.getPhoneNum());
    }

    @Override
    public void loginFaild(UserInfo msg) {
        showMsg("登录失败   "   +msg.getPhoneNum());
    }

    @Override
    public void phoneError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void passwordError(String errorMsg) {
        showMsg(errorMsg);
    }

    @OnClick(R.id.login_submit)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.login_submit:
                String phoneNum=loginPhone.getText().toString().trim();
                String password=loginPwd.getText().toString().trim();
                if(mController.checkPhoneAndPwd(phoneNum,password)){
                    mController.LoginMethod(phoneNum,password);
                }
                break;
            default:
                break;
        }
    }
}
